package net.bw.realtime.jtp.dws.log.job;

import com.alibaba.fastjson.JSON;
import net.bw.realtime.jtp.common.utils.JdbcUtil;
import net.bw.realtime.jtp.common.utils.KafkaUtil;
import net.bw.realtime.jtp.dws.log.bean.PageViewBean;
import net.bw.realtime.jtp.dws.log.function.PageViewBeanMapFunction;
import net.bw.realtime.jtp.dws.log.function.PageViewReportReduceFunction;
import net.bw.realtime.jtp.dws.log.function.PageViewReportWindowFunction;
import net.bw.realtime.jtp.dws.log.function.PageViewWindowFunction;
import org.apache.flink.api.common.eventtime.SerializableTimestampAssigner;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.datastream.WindowedStream;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.windowing.assigners.TumblingEventTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;
import java.time.Duration;

/**
 * @author liuyawei
 * @date 2025/5/19
 */
public class JtpLogPageViewMinuteWindowDwsJob {

    public static void main(String[] args) throws Exception {

        // 1. 创建执行环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        // 2. 设置并行度
        env.setParallelism(1);

        // 3. 读取数据
        DataStream<String> pageStream = KafkaUtil.consumerKafka(env, "dwd-traffic-page-log");
        //pageStream.print();

        // 4. 转换数据
        DataStream<String> processStream = process(pageStream);

        // 5. 输出数据
//        processStream.print();

        // 6.存储clickhouse
        JdbcUtil.sinkToClickhouseUpsert(
                processStream,
                "INSERT INTO jtp_log_report.dws_traffic_page_view_window_report(\n" +
                        "    window_start_time, window_end_time,\n" +
                        "    province, brand, channel,is_new,\n" +
                        "    pv_count, pv_during_time, uv_count, session_count,\n" +
                        "    ts\n" +
                        ")\n" +
                        "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"
        );

        // 7.启动程序
        env.execute("JtpLogPageViewMinuteWindowDwsJob");
    }

    private static DataStream<String> process(DataStream<String> pageStream) {

        // 1.根据设备ID分组
        KeyedStream<String, String> midStream = pageStream.keyBy(
                json -> JSON.parseObject(json).getJSONObject("common").getString("mid")
        );

        // 2.封装对象
        SingleOutputStreamOperator<PageViewBean> beanStream = midStream.map(new PageViewBeanMapFunction());

        // 3.设置时间水位线
        SingleOutputStreamOperator<PageViewBean> watermarksStream = beanStream.assignTimestampsAndWatermarks(
                WatermarkStrategy
                        // 设置延迟时间
                        .<PageViewBean>forBoundedOutOfOrderness(Duration.ofSeconds(0))
                        .withTimestampAssigner(
                                new SerializableTimestampAssigner<PageViewBean>() {
                                    @Override
                                    public long extractTimestamp(PageViewBean element, long recordTimestamp) {
                                        return element.getTs();
                                    }
                                }
                        )
        );

        // 4.按照品牌、区域、渠道、新老用户分组
        KeyedStream<PageViewBean, String> keyedStream = watermarksStream.keyBy(
                bean -> bean.getProvince() + "," + bean.getBrand() + "," + bean.getChannel() + "," + bean.getIsNew()
        );

        // 5.开窗，滚动窗口，窗口事件为1分钟
        WindowedStream<PageViewBean, String, TimeWindow> windowedStream = keyedStream.window(
                TumblingEventTimeWindows.of(
                        Time.minutes(1)
                )

        );

        // 6.调用 聚合统计 全量计算
        DataStream<String> reportStream = windowedStream.apply(new PageViewWindowFunction());

        // 7.增量计算
        windowedStream.reduce(
                new PageViewReportReduceFunction(), new PageViewReportWindowFunction()
        );

        // 7.返回结果
        return reportStream;

    }

}
